# filter companies for blue-green deployment

=begin

files:
proxy_access.log.2020-08-28-15.sh-api-gw002.gz
proxy_access.log.2020-08-28-15.sh-api-gw003.gz

bash commands

# zgrep -Eo ' "[[:digit:]]+" ' proxy_access.log.2020-08-28-15.sh-api-gw002.gz | grep -Eo '[[:digit:]]+' > /tmp/tids_stat_2020_08_28_002.log
# zgrep -Eo ' "[[:digit:]]+" ' proxy_access.log.2020-08-28-15.sh-api-gw003.gz | grep -Eo '[[:digit:]]+' > /tmp/tids_stat_2020_08_28_003.log

# cat tids_stat_2020_08_28_002.log tids_stat_2020_08_28_003.log > /tmp/tids_stat_0828.log

# wc -l /tmp/tids_stat_0828.log   # 6484426

# sort -n /tmp/tids_stat_0828.log | uniq -c | sort -nr > /tmp/tids_stat_0828_freq.log

=end


def get_candidates(file_name:, total: )
  freq_by_tid = File.foreach(file_name).each_with_object({}) do |line, h|
    v, k = line.chomp.lstrip.split(" ").map(&:to_i)
    h[k] = v
  end

  ratio_by_tid = freq_by_tid.each_with_object({}) do |(k, v), h|
    h[k] = v.fdiv(total)
  end

  hash = ratio_by_tid.reject { |k, _| k == 0 }

  detailed_candidates = hash.each_with_object({}) do |(tid, ratio), h|
    on_shard_by_company_id(tid) do
      com = Company.find(tid)
      h[tid] = {}
      h[tid]["subdomain"] = com.subdomain
      h[tid]["ratio"] = ratio * 100
      h[tid]["shard_id"] = ActiveRecord::Base.current_shard_id
      h[tid]["company_name"] = com.company_name
      h[tid]["agent_count"] = com.agents.count
      h[tid]["tid"] = tid
    end
  end

  de = detailed_candidates
  _50_percent_tenants = select_50_percent(de)

  puts "50 percent:"
  print_list(_50_percent_tenants)
end

def select_50_percent(de)
  results = []
  results += de.select { |k, _| cck_allow_ids.include?(k) }.values
  puts
  puts "==size of cck_allow_ids is #{cck_allow_ids.size}"
  puts "==size of pre-defined tenants is #{results.size}"

  de.reject { |k, _| block_ids.include?(k) }.values.first(100).shuffle.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 50
      results << h
    end
  end
  results
end

def print_list(array)
  puts
  puts "----"

  array.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  puts "----"
  puts
end

def key_customer_subdomains
  %w(
    xinliu
    oristartech
    jiangroom
    4px
    kuaishouhr
    feihe
    relxtech
    oyohotels
    consumerservice
    wxbio
    kaipujushi
    jiahui
    mengniu
    zejipx
    1396599
    sce
    1349541
    hyfxdh
    hyxfzy
    allegion
    peacebird
    yingdajincailvxingshe
    wens
    xtransfer
    zbom
    1359004
  )
end

def cck_block_subdomains
  %w(
    haier
    1520026
  )
end

def cck_allow_subdomains
  %w(
    xianghuanji
    www-wodeev
    1526377
    1865992
    taoyuanju
    lemonbox
    1869276
    1551411
    kunapp
    zmarthome
    1897975
    4001832999
    guinianggroup
    greedc
    aldics
    1560469
    loveislove
    1589354
    1384970
    1855343
    1774891
    1772933
    1567999
    1399544
    1991986
    1588809
    1800308
    15910680789
    1766171
    1376382
    test0619
    airmb
    test0622
    henanjichang1
    1867220
    1800471
    1364537
    1584022
    1319228
    haoweilai
    1384021
    1762740
    daikin
    wnkj
    jhk
    jiaofuzhongxin
    popmart
    phytium
    1899687
    shqlxx
    1577894
    test0805kun
    1335780
    1892431
    wicresoft-uat
    ztjs1
    1864170
    lefucake
    1555298
    apcsr
  )
end

def key_ids
  [16096,
    16323,
    5037,
    2599,
    11758,
    22346,
    2559,
    4382,
    4386,
    3887,
    1403,
    5496,
    6859,
    11027,
    1685,
    12955,
    6126,
    15936,
    7194,
    3178,
    2600,
    10566,
    6131,
    8419,
    5111,
    17526]
end

def cck_block_ids
  [26468, 26159]
end

def cck_allow_ids
  [26695,
    26740,
    26642,
    26452,
    26698,
    25775,
    26084,
    26724,
    10298,
    26774,
    26598,
    26055,
    26727,
    25762,
    25237,
    26665,
    24966,
    26703,
    24432,
    26044,
    25384,
    26259,
    22535,
    25389,
    26757,
    26602,
    25445,
    24557,
    26745,
    26407,
    16007,
    26141,
    26697,
    23982,
    26775,
    26706,
    26704,
    25844,
    22254,
    25685,
    26534,
    17389,
    26714,
    20540,
    26767,
    1405,
    21113,
    26722,
    26719,
    26726,
    26450,
    26465,
    26507,
    26739,
    26749,
    26705,
    5823,
    6735,
    25958,
    26755]
end

def special_ids
  [1, 7]
end

def block_ids
  key_ids + special_ids + cck_block_ids
end

def huge_traffic_ids
  # 13640, 1361514, 13.2% traffic ratio, 上海韩众网络科技有限公司/上海摄图
  # 4874, dzyxedu, 5.66% traffic ratio, 河南高辉教育科技有限公司/河南省华辉计算机
  # 26468, 1520026, xx% 企查查科技有限公司
  # [13640, 4874, 26468]
  []
end

get_candidates(file_name: "/tmp/freq.log", total: 10316919)
